<?php

namespace app\api\model;

use think\Model;

class ImBlack extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:添加黑名单
     * User: tangfen
     * Date: 2023/12/21
     * Time: 20:48
     */
    public function addBlack($uid, $black_uid)
    {
        $result = $this->where(['uid' => $uid, 'black_uid' => $black_uid])->value('id');
        if (empty($result)) {
            //不存在黑名单时添加记录
            $this->data([
                'uid' => $uid,
                'black_uid' => $black_uid,
            ]);
            $result = $this->save();
            if ($result) {
                return true;
            } else {
                return false;
            }
        } else {
            $this->where(['uid' => $uid, 'black_uid' => $black_uid])->update(['status' => 1, 'update_time' => time()]);// 1是, 2否
            return true;
        }
    }

    /**
     * Notes:删除黑名单
     * User: tangfen
     * Date: 2023/12/21
     * Time: 20:39
     * @param $params
     * @return bool
     */
    public function DelBlack($uid, $black_uid)
    {

        $result = $this->where(['uid' => $uid, 'black_uid' => $black_uid])->update(['status' => 2,'delete_time'=>time()]);// 1是, 2否
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Notes:获取黑名单列表
     * User: tangfen
     * Date: 2023/12/21
     * Time: 20:39
     * @param $where
     * @param $params
     */
    public function GetBlackList($params)
    {
        $where = [
            'b.uid' => $params['uid'],
            'b.status' => 1
        ];

        $list = ImBlack::alias('b')
            ->join('user u', 'u.id=b.black_uid', 'LEFT')
            ->where($where)
            ->field('u.*, b.id as b_id')
            ->order('b.id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);
        return $list;
    }


}